angular.module('SmartCanteenApp.financialManage', ['ngRoute', 'ngTouch'])

	.config(['$routeProvider', function($routeProvider) {
		$routeProvider.when('/dayReport', {
			templateUrl: 'financial/dayReport.html',
			controller: 'dayReportCtrl'
		});
		$routeProvider.when('/sendBack', {
			templateUrl: 'financial/sendBack.html',
			controller: 'sendBackCtrl'
		});
		$routeProvider.when('/billData', {
			templateUrl: 'financial/billData.html',
			controller: 'billDataCtrl'
		});
	}])
	//日报控制器
	.controller('dayReportCtrl', ['$scope', '$timeout', function($scope, $timeout) {
		//获取时间
		$scope.getNowFormatDate = function() {
			var date = new Date();
			var seperator1 = "-";
			var seperator2 = ":";
			var month = date.getMonth() + 1;
			var strDate = date.getDate();
			if(month >= 1 && month <= 9) {
				month = "0" + month;
			}
			if(strDate >= 0 && strDate <= 9) {
				strDate = "0" + strDate;
			}
			var now = date.getFullYear() + seperator1 + month + seperator1 + strDate;

			return now;
		}

		$('#datepicker').datepicker({
			autoclose: true
		});
		//日报查询条件对象
		$scope.searchReportBy = {
			"canteen": "全部食堂",
			"query_terms": "按档口",
			"time": $scope.getNowFormatDate()
		}
		//根据默认条件查询数据
		$scope.data = {
			"canteen": [{
				"name": "食堂1",
				"id": "1"
			}, {
				"name": "食堂2",
				"id": "2"
			}, {
				"name": "食堂3",
				"id": "3"
			}],
			"rows": [
				[
					"档口1",
					"2",
					"1",
					"3"
				],
				[
					"档口2",
					"4",
					"4",
					"4"
				],
				[
					"档口3",
					"6",
					"7",
					"8"
				],

			]
		}
		//分页
		$scope.initial = '10'; //设置默认每页条数
		$scope.pagination([]); //清空容器
		$scope.pagination($scope.data.rows); //传入数据
		//回传条件并获取数据
		$scope.searchReport = function() {
			console.log(this.searchReportBy)
			$scope.getDayReport = function(){
		    	$scope.getJson($scope.resultPath+'/wisdomeatery/v1/canteens/'+$scope.canteenId+'/reports','','get',function(data){
					console.log(data)
				})
		    }
		   
	   		if( $scope.getCanteenId() != ''){
	   				$scope.canteenId =  $scope.getCanteenId() ;
	   				//获取基础数据
			 		$scope.getDayReport();
	   		}
			$scope.$on('to-child', function(event,data) {
				//获取从父控制器传来的当前选中食堂
				$scope.canteenId = data;
				$scope.getDayReport();
			});
//			$scope.data = {
//				"canteen": [{
//					"name": "食堂1",
//					"id": "1"
//				}, {
//					"name": "食堂2",
//					"id": "2"
//				}, {
//					"name": "食堂3",
//					"id": "3"
//				}],
//				"rows": [
//					[
//						"类别1",
//						"2",
//						"1",
//						"3"
//					],
//					[
//						"类别1",
//						"4",
//						"4",
//						"4"
//					],
//					[
//						"类别1",
//						"6",
//						"7",
//						"8"
//					],
//					[
//						"类别1",
//						"6",
//						"7",
//						"8"
//					],
//
//				]
//			}
			$scope.pagination([]); //清空容器
			$scope.pagination($scope.data.rows); //传入数据
		}

	}])
	.controller('sendBackCtrl', ['$scope', '$timeout', function($scope, $timeout) {
		$scope.getNowFormatDate = function() {
			var date = new Date();
			var seperator1 = "-";
			var seperator2 = ":";
			var month = date.getMonth() + 1;
			var strDate = date.getDate();
			if(month >= 1 && month <= 9) {
				month = "0" + month;
			}
			if(strDate >= 0 && strDate <= 9) {
				strDate = "0" + strDate;
			}
			var now = date.getFullYear() + seperator1 + month + seperator1 + strDate;

			return now;
		}
		$('#reservation').daterangepicker();
		//发卡退卡记录查询对象
		$scope.details = false
		$scope.searchSendBackobj = {
			"start_time": $scope.getNowFormatDate(),
			"end_time": $scope.getNowFormatDate(),
			"query_terms": "按汇总"
		}
		//根据$scope.searchSendBackobj发送查询请求
		$scope.callBackData = [{
			"summary": [{
				"operator": "李",
				"open_num": "2",
				"back_num": "1"
			}],
			"details": [{
				"service_type": "退卡",
				"card_id": "12",
				"card_type": "普卡",
				"username": "ding",
				"operator": "lee",
				"time": "2017-1-14"
			}, {
				"service_type": "退卡",
				"card_id": "12",
				"card_type": "普卡",
				"username": "ding",
				"operator": "lee",
				"time": "2017-1-14"
			}, ]
		}]
		//分页
		$scope.initial = '10'; //设置默认每页条数
		$scope.pagination([]); //清空容器
		$scope.pagination($scope.callBackData[0].summary); //传入数据
		//。。。。。。。。
		$scope.dateStr = $scope.getNowFormatDate() + ' ' + '至' + ' ' + $scope.getNowFormatDate()
		$scope.searchSendBack = function() {
			var arr = $scope.dateStr.split('至');
			this.searchSendBackobj.start_time = arr[0]
			this.searchSendBackobj.end_time = arr[1]

			console.log(this.searchSendBackobj) //回传
			//获取查询数据
			$scope.callBackData = [{
				"summary": [{
					"operator": "lee",
					"open_num": "2",
					"back_num": "1"
				}],
				"details": [{
					"service_type": "退卡",
					"card_id": "12",
					"card_type": "普卡",
					"username": "ding",
					"operator": "lee",
					"time": "2017-1-14"
				}]
			}]
			if(this.searchSendBackobj.query_terms == '按汇总') {
				$scope.details = false
				$scope.pagination([]); //清空容器
				$scope.pagination($scope.callBackData[0].summary); //传入数据
			} else {
				$scope.details = true
				$scope.pagination([]); //清空容器
				$scope.pagination($scope.callBackData[0].details); //传入数据
			}
		}
		$scope.downloadtwo = function() {
			var outby = this.searchSendBackobj.query_terms
			//  			console.log(outby)
			if(outby == '按汇总') {
				$scope.download('according_all')
			} else if(outby == '按明细') {
				$scope.download('according_details')
			}
		}
		//导出数据
		$scope.downloadSendBack = function(tiaoJian) {
			if(tiaoJian == '按明细') {
				$scope.download("tb_sendbackByDetails", "发卡退卡", "按明细")
			} else {
				$scope.download("tb_sendbackByNum", "发卡退卡", "按汇总")
			}
		}
	}])
	.controller('billDataCtrl', ['$scope', '$timeout', function($scope, $timeout) {

		$scope.billHeader = ["账单号", "财务日期", "用户姓名", "用户手机", "部门名称", "卡号", "卡类名称", "食堂名称", "餐别名称", "应收金额", "优惠金额", "消费总数", "付款名称", "付款金额", "充值使用", "餐补使用", "付款时间", "备注", "是否退掉", "关联退单单号", "关联预订单号", "单据类型", "操作人", "操作时间"]
		$('#reservation').daterangepicker();
		//当前时间获取
		$scope.getNowFormatDate = function() {
			var date = new Date();
			var seperator1 = "-";
			var seperator2 = ":";
			var month = date.getMonth() + 1;
			var strDate = date.getDate();
			if(month >= 1 && month <= 9) {
				month = "0" + month;
			}
			if(strDate >= 0 && strDate <= 9) {
				strDate = "0" + strDate;
			}
			var now = date.getFullYear() + seperator1 + month + seperator1 + strDate;

			return now;
		}
		//获取所有食堂信息
		$scope.canteens = [{
			"id": 1,
			"name": "食堂一",
		}, {
			"id": 2,
			"name": "食堂二",
		}, ]
		//获取所有餐别信息 id和name
		//获取全部部门 id和name
		//账单查询查询条件对象
		$scope.searchBillBy = {
			"canteen": "全部食堂",
			"food_time": "所有餐别",
			"department": "所有部门",
			"user_information": "",
			"user_card": "",
			"start_time": $scope.getNowFormatDate(),
			"end_time": $scope.getNowFormatDate(),
			"bill_type": "全部单据",
			"payment": "全部付款方式",
			"query_terms": "按单头"
		}
		//根据默认查询对象返回数据
		$scope.billData = [{
			"bill_id": 27,
			"bill_time": "11",
			"username": "mock",
			"telephone": "mock",
			"department_name": "mock",
			"card_id": "mock",
			"card_name": "mock",
			"canteen": "mock",
			"food_time": "mock",
			"receivables": "mock",
			"discount": "mock",
			"consumption": "mock",
			"payment": "mock",
			"payment_amount": "mock",
			"recharge_use": "mock",
			"subsidy_use": "mock",
			"payment_time": "mock",
			"remark": "mock",
			"chargeback": false,
			"chargeback_bill": "mock",
			"reserve_bill": "mock",
			"bill_type": "mock",
			"operator": "mock",
			"operating_time": "mock"
		}]
		//设置初始时间为当前日期
		$scope.billDate = $scope.getNowFormatDate() + ' ' + '至' + ' ' + $scope.getNowFormatDate()
		//发起查询动作
		$scope.searchBillData = function() {
			//时间设置
			var arr = $scope.billDate.split('至');
			this.searchBillBy.start_time = arr[0]
			this.searchBillBy.end_time = arr[1]

			console.log(this.searchBillBy)
		}
		//分页
		$scope.pagination([]); //清空容器
		$scope.initial = '10'; //设置默认每页条数
		$scope.pagination($scope.billData); //传入数据

	}])